//
//  Settings.m
//  PartyAnimal
//
//  Created by Hilary Muludiang on 4/1/12.
//  Copyright 2012 Muludiang. All rights reserved.
//

#import "Settings.h"
#import "LoginViewController.h"
#import "StackMob.h"
#import "iBacchanal.h"
#import "Reachability.h"

@implementation Settings

@synthesize versionBuild;
@synthesize feedbackFeatures;

-(void)reachabilityChanged:(NSNotification *)note{
	
	Reachability* curReach = [note object];
	NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
	
	NetworkStatus netStatus = [curReach currentReachabilityStatus];
	//BOOL connectionRequired = [curReach connectionRequired];
	//NSLog(@"status: %@ ", connectionRequired);
	
	iBacchanal *bacchanal = [iBacchanal sharedUser];
	
	switch (netStatus){
		case NotReachable:{
			//show an alert to let the user know that they can't connect...
			UIAlertView *alert = [[[UIAlertView alloc] initWithTitle: nil message: bacchanal.downMessage
															delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
			[alert show];
			bacchanal.networkUp = NO;
			self.navigationItem.rightBarButtonItem.enabled = NO;
			break;
		}
		default:{
			// Network is reachable
			UIAlertView *alert = [[[UIAlertView alloc] initWithTitle: nil message: bacchanal.upMessage
															delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
			[alert show];
			self.navigationItem.rightBarButtonItem.enabled = YES;
			
			bacchanal.networkUp = YES;			
			break;
		}
			
    }
}


-(IBAction)loginLogout:(UIButton *)sender{
	
	iBacchanal *bacchanal = [iBacchanal sharedUser];
	
	if (!bacchanal.loggedIn){
		[TestFlight passCheckpoint:@"Accessed login view"];
		LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:@"Login" bundle:nil]; 
		UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginView];
		[loginView release];
		
		navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
		[self presentModalViewController:navController animated:YES];
		[navController release];
	}
	else{
		[[StackMob stackmob] logoutWithCallback:^(BOOL success, id result) {
			if (success) {
				bacchanal.loggedIn = NO;
				bacchanal.currentUser = nil;
				self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStylePlain target:self action:@selector(loginLogout:)];
				
			} else {
				// logout failed
			}
		}];
	}
}


-(IBAction)launchFeedback:(UIButton *) sender {
    [TestFlight openFeedbackView];
}
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationItem.title = @"Account Settings";
}


-(void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:YES];
	
	versionBuild.text = [NSString stringWithFormat:@"Version %@ (Build %@)",
						 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
	
	iBacchanal *bacchanal = [iBacchanal sharedUser];
	if (bacchanal.loggedIn)
		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(loginLogout:)];
	else
		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStylePlain target:self action:@selector(loginLogout:)];
	
	if (!bacchanal.networkUp)
		self.navigationItem.rightBarButtonItem.enabled = NO;
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(reachabilityChanged:) 
												 name:kReachabilityChangedNotification 
											   object:nil];	
}

-(void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES; // (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[feedbackFeatures release];
	[versionBuild release];
    [super dealloc];
}


@end
